/* eslint-disable @typescript-eslint/indent */
import * as File from './file'
import { APIResult, success, fail } from './types'
import { Router } from 'express'

const router = Router()

router.get('/:lang/*', (req, res) => {
    // check
    const _ = req.params
    // console.log(_)
    const lang = (_.lang == '_') ? 'zh_CN' : _.lang
    const path = _[0]
    if (!lang || !path)
        res.send(fail('Invalid arguments.'))
    else if (!File.ValidLangs.includes(lang))
        res.send(fail('No such language.'))
    else if (path && path.match(/[^A-Za-z0-9-\_\@\/]+/))
        res.send(fail('Invalid xpath.'))
    else {
        try {
            const result = File.getLevels(path, lang)
            if (result)
                res.send(success(result))
            else 
                res.send(fail('Invalid path.'))
        } catch (e) {
            res.send(fail(`Error: ${e}`))
        }
    }
})

export default router
